#import "JdsdkPlugin.h"
#import "JDSDK/JDKeplerSDK.h"

@interface JdsdkPlugin()
@end

@implementation JdsdkPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"jdsdk" binaryMessenger:[registrar messenger]];
  JdsdkPlugin* instance = [[JdsdkPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  if ([@"init" isEqualToString:call.method]) {
      NSString *appKey = call.arguments[@"appKey"];
      NSString *appSecret = call.arguments[@"appSecret"];
      [[KeplerApiManager sharedKPService]asyncInitSdk:appKey secretKey:appSecret sucessCallback:^(){
          result(@"{status:1}");
      }failedCallback:^(NSError *error){
          result(@"{status:0}");
      }];
  } else if([@"openUrl" isEqualToString:call.method]){
      NSString *url = call.arguments[@"url"];
      //判断是否安装京东app
      if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:[NSString stringWithFormat:@"openapp.jdmobile://"]]]) {
          [[KeplerApiManager sharedKPService]openKeplerPageWithURL:url userInfo:nil successCallback:^{
              result(@"{status:1}");
        } failedCallback:^(NSInteger code, NSString * _Nonnull url) {
            result(@"{status:0}");
        }];
      }else{
          result(@"{status:0}");
      }
  } else {
    result(FlutterMethodNotImplemented);
  }
}

@end
